//
//  XKOMainTabBar.swift
//  weibofirst
//
//  Created by Mars on 16/4/9.
//  Copyright © 2016年 Mars. All rights reserved.
//

import UIKit

class XKOMainTabBar: UITabBar {

    
    //创建闭包属性
    var composeClosure:(()->())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        //每个按钮的宽度
        let width = frame.size.width / CGFloat(mainTabBatButtoncount)
        
        var index = 0
        
        for item in subviews{
            //判断subviews中的button是否是UITabBarButton类型
            if item .isKindOfClass (NSClassFromString("UITabBarButton")!) {
            
                item.frame = CGRect(x:width * CGFloat(index) , y: 0, width: width, height: frame.size.height)
                
                index++;
                
                //如果下标等于2的时候,空出下标2的位置
                if(index == 2){
                
                    index++
                }
            }
        }
        
        composeButton.frame = CGRect(x: width * 2, y: 0, width: width, height: frame.size.height)
        
    }
    
    //加号点击触发的方法
  @objc private func addBtnClick(){
    
        composeClosure?()
    
    }

    //MARK:懒加载加号按钮
   private lazy var composeButton : UIButton = {
    
        let button = UIButton()
    
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
    
        //监听加号属性
        button.addTarget(self, action: "addBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
       self.addSubview(button)
        
        return button
    }()
}
